import java.io.*;
import java.util.*;
import java.text.*;

public class AnalyzeFilesWithExistFile1 {

	static PrintWriter out;
	static PrintWriter outDiff;
	static long save = 0;
	static String e="second/";
	static String cmpDirName="/should/not/be/blank/path";
	static int minsz = 0;
	static long maxsz = Long.MAX_VALUE;
	static TreeMap<Long, List<String>> efilemap =new TreeMap<Long, List<String>>(); 
	static List<String> remFiles =new ArrayList<String>();
	static boolean ignorefilename=true;
	static boolean exhaustive=false;
	public static void main(String[] args) throws Exception {
		if(args.length>0){
				cmpDirName=args[0];
		}
		if(args.length>1){
			if(args[1].startsWith("f"))
				ignorefilename=false;
		}
		if(args.length>2){
			if(args[2].startsWith("t"))
				exhaustive=true;
		}
		if(args.length>3){
			minsz=Integer.parseInt(args[3]);
		}
		if(args.length>4){
			maxsz=Long.parseLong(args[4]);
		}
		
		out = new PrintWriter(new File(e+"files2.log"));
		outDiff = new PrintWriter(new File(e+"rm2_files.sh"));
		StringBuffer _dirs_out2 = new StringBuffer("");
		BufferedReader _br = new BufferedReader(new InputStreamReader(
				new FileInputStream(e+"dirs1.sort.log")));
		List<String> dirs = new ArrayList<String>();
		String _li = "";
		while ((_li = _br.readLine()) != null) {
			_dirs_out2.append(_li);
			if (_li.startsWith("meld \"")) {
				int _o = _li.indexOf("\" \"");
				dirs.add(_li.substring(6, _o));
				dirs.add(_li.substring(_o + 3, _li.length() - 1));
			}
		}
		_br.close();
		out.println("-----------------");
		String dirs_out2 = _dirs_out2.toString();

		File e_dir = new File(e+"/");
		File[] efls = e_dir.listFiles();

		File _dir = new File(".");
		File[] fls = _dir.listFiles();

		TreeMap<Long, List<String>> filemap = new TreeMap<Long, List<String>>();
		filemap.clear();
		for (File f : fls) {
			if (f.getName().endsWith(".f1.txt")) {

				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(f)));
				String line = "";
				while ((line = br.readLine()) != null) {
					line=decrypt(line);
					int i = line.lastIndexOf("::");
					Long size = Long.parseLong(line.substring(i + 2));
					if (size < minsz || size > maxsz)
						continue;
					String _file = line.substring(0, i);
					boolean _f = false;
						if (_file.startsWith(cmpDirName + "/")) {
							continue;
						}
					size = 999999999999999L - size;
					List<String> _files = filemap.get(size);
					if (_files == null)
						_files = new ArrayList<String>();
					_files.add(_file);
					filemap.put(size, _files);
				}
			}
		}


		
		efilemap.clear();
		for (File f : efls) {
			if (f.getName().endsWith(".f1.txt")) {

				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(f)));
				String line = "";
				while ((line = br.readLine()) != null) {
					line=decrypt(line);
					int i = line.lastIndexOf("::");
					Long size = Long.parseLong(line.substring(i + 2));
					String _file = line.substring(0, i);
					boolean _f = false;
					for (String _d : dirs) {
						if (_file.startsWith(_d + "/")) {
							_f = true;
							break;
						}
					}
					if (_f)
						continue;
					size = 999999999999999L - size;
					List<String> _files = efilemap.get(size);
					if (_files == null)
						_files = new ArrayList<String>();
					_files.add(_file);
					efilemap.put(size, _files);
				}

				Set<Long> keys = efilemap.keySet();
				for (Long key : keys) {
					outDiff.flush();
					List<String> vals = filemap.get(key);
					List<String> evals = efilemap.get(key);
					if (evals!= null &&  vals !=null && evals.size() > 0 && vals.size() > 0) {
						for(String val: vals){
							for (int ii=0;ii<evals.size();ii++) {
								String eval=evals.get(ii);
								out.println(eval);
								if (ignorefilename || val.substring(val.lastIndexOf("/")+1).equals(eval.substring(eval.lastIndexOf("/")+1))){
									outDiff.println("echo \"" + val + "\" \""
											+ eval + "\"");
									outDiff.println("diff \"" + val + "\" \""
										+ eval + "\"");
									remFiles.add(eval);
									if(!exhaustive){
										evals.remove(ii);
										ii--;
									}
								}
								save += ((999999999999999L - key) * (vals.size() - 1));
							}
						}
					}
				}

			}
		}
		out.println("\n\nSAVINGS=" + save);
		out.close();
		outDiff.close();
	}

	
	public static Properties loadProperties(String fileName) throws Exception{
	ByteArrayOutputStream bao=new ByteArrayOutputStream();
	FileInputStream fis=new FileInputStream(fileName);
	Crypto.getInstance().decrypt(fis, bao);
	byte[] barr=bao.toByteArray();
	ByteArrayInputStream bis= new ByteArrayInputStream(barr);
	Properties props= new Properties();
	props.loadFromXML(bis);
	bao.close();
	fis.close();
	return props;
}
	public static String encrypt(String str){
		return Crypto.getInstance().encrypt(str);
	}
	public static String decrypt(String str){
		return Crypto.getInstance().decrypt(str);
	}

}
